/*
 * Created on May 8, 2005
 *
 */
package org.mushroomdb.catalog.impl;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.mushroomdb.catalog.Column;
import org.mushroomdb.catalog.Register;
import org.mushroomdb.catalog.WriteBackRegister;
import org.mushroomdb.filemanagement.Page;


/**
 * @author Matias
 *
 */
public class FilteredRegister implements WriteBackRegister {
	private WriteBackRegister register;
	private boolean defaultValue;
	private List values;

	/**
	 * 
	 */
	public FilteredRegister(WriteBackRegister register, boolean defaultValue) {
		this.register = register;
		this.defaultValue = defaultValue;
		this.values = new LinkedList();
		Iterator columns = register.getColumns();
		while (columns.hasNext()) {
			Column column = (Column) columns.next();
			Object[] item = new Object[] {
					column,
					register.getValue(register.getColumnIndex(column)),
					new Boolean(defaultValue)};
			this.values.add(item);
		}
	}
	
	public void setColumnFilter(List columns, boolean filter) {
		Iterator iterator = columns.iterator();
		while (iterator.hasNext()) {
			this.setColumnFilter((Column) iterator.next(),filter);
		}
	}

	public void setColumnFilter(Column column, boolean filter) {
		Iterator values = this.values.iterator();
		while (values.hasNext()) {
			Object[] item = (Object[]) values.next();
			if (item[0].equals(column)) {
				item[2] = new Boolean(filter);
			}
		}
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumnNames()
	 */
	public Iterator getColumns() {
		return this.getFilteredList().iterator();
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumnIndex(java.lang.String)
	 */
	public int getColumnIndex(Column column) {
		Iterator columns = this.getFilteredList().iterator();
		int index = 1;
		while (columns.hasNext()) {
			Column registerColumn = (Column) columns.next();
			if (registerColumn.equals(column)) {
				return index;
			}
			index++;
		}
		return -1;
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getValue(int)
	 */
	public Object getValue(int columnIndex) {
		Column column = (Column) this.getFilteredList().get(columnIndex - 1);
		return this.register.getValue(this.register.getColumnIndex(column));
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumnCount()
	 */
	public int getColumnCount() {
		return this.getFilteredList().size();
	}
	
	private List getFilteredList() {
		List filteredColumns = new LinkedList();
		Iterator iterator = this.values.iterator();
		while (iterator.hasNext()) {
			Object[] item = (Object[]) iterator.next();
			if (item[2].equals(Boolean.TRUE)) {
				filteredColumns.add(item[0]);
			}
		}
		return filteredColumns;
	}

	/**
	 * @see org.mushroomdb.catalog.Register#setValue(int, java.lang.Object)
	 */
	public void setValue(int columnIndex, Object value) {
		Column column = (Column) this.getFilteredList().get(columnIndex - 1);
		this.register.setValue(this.register.getColumnIndex(column),value);
	}
	
	/**
	 * toString
	 */
	public String toString() {
		int columnCount = this.getColumnCount();
		String rv = ""; //$NON-NLS-1$
		
		for (int i = 1; i <= columnCount; i++) {
			if (rv.length() > 0) {
				rv = rv + ',';
			}
			
			Object object = this.getValue(i).toString();
			if (object instanceof String) {
				rv = rv + '\'' + object.toString() + '\'';
			} else {
				rv = rv + object.toString();
			}
		}
		
		return '(' + rv + ')';
	}
	
	
	/**
	 * @see org.mushroomdb.catalog.Register#remove()
	 */
	public void remove() {
		this.register.remove();
	}

	 /* (non-Javadoc)
     * @see org.mushroomdb.catalog.WriteBackRegister#setPage(org.mushroomdb.filemanagement.Page)
     */
    public void setPage(Page page) {
        this.register.setPage(page);
    }

    /* (non-Javadoc)
     * @see org.mushroomdb.catalog.WriteBackRegister#setOffset(int)
     */
    public void setOffset(int offset) {
        this.register.setOffset(offset);
    }

    /* (non-Javadoc)
     * @see org.mushroomdb.catalog.WriteBackRegister#getOffset()
     */
    public int getOffset() {
         return this.register.getOffset();
    }

    /* (non-Javadoc)
     * @see org.mushroomdb.catalog.WriteBackRegister#getPage()
     */
    public Page getPage() {
        return this.register.getPage();
    }

    /* (non-Javadoc)
     * @see org.mushroomdb.catalog.WriteBackRegister#isInTrx()
     */
    public boolean isInTrx() {
       return this.register.isInTrx();
    }
}
